package com.untrance.shop.core.joiner;

import com.google.appengine.api.datastore.Key;
import com.untrance.shop.common.vo.AbstractVO;
import com.untrance.shop.model.MainEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * @author eugene zadyra
 */
public abstract class AbstractJoiner<Primary, JoinVO extends AbstractVO> {
    protected List<JoinVO> resultList;

    public List<JoinVO> joinAll(List<Primary> primaryList) {
        if(primaryList==null){
            return null;
        }
        resultList = new ArrayList<JoinVO>();
        for (Primary p : primaryList){
            resultList.add(join(p));
        }
        return resultList;
    }

    public abstract JoinVO join(Primary value);

    protected <T extends MainEntity> T findDependentEntity(List<T> where, Key key){
        if(where==null){
            return null;
        }
        for (T t:where){
            if(key.equals(t.getId())){
                return t;
            }
        }
        return null;
    }
}
